package bom.pojo.organization;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * @Author: Fancyears·Maylos·Malvis
 * @Description: 用户实体类，用于操作用户登录
 * @Date: Created in  2018/11/27 14:48
 * @Modified By:
 */
@Data
public class User implements UserDetails {
    private static final long serialVersionUID = 998765434567839407L;
    /**
     * 主键
     */
    private Long id;
    /**
     * 登录名
     */
    private String login;
    /**
     * 新建的时候回从前端进行MD5加密后返回，后台再次加密存储到数据库中
     */
    private String password;
    /**
     * 用户名
     */
    private String userName;
    /**
     * 地址，可以不需要
     */
    private String address;
    /**
     * 职位
     */
    private String job;
    /**
     * 所在的组织
     */
    private long groupId;
    /**
     * 状态，1为可用，0为禁用，禁用则不能登录
     */
    private String state;
    private String type;
    /**
     * 上一次登录时间
     */
    private Date lastLoginDate;
    /**
     * 用户角色信息
     */
    private List<UserRole> roles;
    /**
     * 权限集合数据
     */
    private String roleArray;
    /**
     * 所在分组的集合
     */
    private OrgGroup orgGroup;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 员工工号
     */
    private String userNo;

    public User() {
        super();
    }

    public User(Long id) {
        this.id = id;
    }

    public static String reflectToDBField(String code) {
        switch (code) {
            case "userName":
                return "user_name";
            case "groupId":
                return "group_Id";
            case "id":
                return "id";
            default:
                return null;
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
        if (this.getRoles() != null) {
            List<UserRole> roles = this.getRoles();
            for (UserRole role : roles) {
                if (role.getName() != null) {
                    auths.add(new SimpleGrantedAuthority(role.getName()));
                }
            }
        }
        return auths;
    }

    @Override
    public String getUsername() {
        return this.getLogin();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    /**
     * 功能描述：组装角色数据集合
     *
     * @param roleArray
     */
    public void packagingRoles(String roleArray) {
        List<UserRole> roles = new ArrayList<UserRole>();
        if (roleArray != null) {
            UserRole userRole = null;
            for (String roleId : roleArray.split(",")) {
                if (!roleId.isEmpty()) {
                    userRole = new UserRole();
                    userRole.setId(Long.parseLong(roleId));
                    roles.add(userRole);
                }
            }
        }
        this.setRoles(roles);
    }
}